import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter App',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // TextEditingController _unameCtl = TextEditingController();
  TextEditingController _upwdSourceCtl = TextEditingController();
  TextEditingController _upwdNewCtl = TextEditingController();
  TextEditingController _upwdConfirmCtl = TextEditingController();
  GlobalKey _gk = GlobalKey<FormState>();

  // var reg_uname = RegExp(r"^\w{1,10}$");
  var reg_upwdSource = RegExp(r"^\d{6}$");
  var reg_upwdNew = RegExp(r"^\d{6}$");
  var reg_upwdConfirm = RegExp(r"^\d{6}$");

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios_new),
          color: Colors.black,
          onPressed: () {
            print('返回');
          },
        ),
        elevation: 0, //去掉Appbar底部阴影
        title: const Text(
          "修改密码",
          style: TextStyle(
            color: Colors.black,
          ),
        ),
        centerTitle: true,
        backgroundColor: Colors.white,
      ),
      body: Container(
        decoration:
        const BoxDecoration(color: Color.fromRGBO(243, 246, 251, 1)),
        // color: const Color.fromRGBO(243, 246, 251, 1),
        // width: double.infinity,
        // height: double.infinity,
        padding: const EdgeInsets.fromLTRB(0, 10, 0, 0),
        child: Form(
          key: _gk,
          child: Column(
            children: <Widget>[
              Container(
                padding: const EdgeInsets.fromLTRB(15, 0, 15, 0),
                decoration: const BoxDecoration(
                    color: Color.fromRGBO(255, 255, 255, 1)),
                child: TextFormField(
                  controller: _upwdSourceCtl,
                  decoration: const InputDecoration(
                    enabledBorder: InputBorder.none,
                    focusedBorder: InputBorder.none,
                    errorBorder: InputBorder.none,
                    hintText: "请填写旧密码",
                    icon: Text("旧密码"),
                    // filled: true,
                    // fillColor: const Color.fromRGBO(255, 255, 255, 1)
                  ),
                  obscureText: true,
                  validator: (v) {
                    return reg_upwdSource.hasMatch(v!) ? null : "请填写旧密码";
                  },
                ),
              ),
              Container(
                child: const Divider(
                    height: 0,
                    thickness: 1,
                    indent: 15,
                    endIndent: 15,
                    color: Color.fromRGBO(240, 240, 240, 1)),
              ),
              Container(
                padding: const EdgeInsets.fromLTRB(15, 0, 15, 0),
                decoration: const BoxDecoration(
                    color: Color.fromRGBO(255, 255, 255, 1)),
                child: TextFormField(
                  controller: _upwdNewCtl,
                  decoration: const InputDecoration(
                    enabledBorder: InputBorder.none,
                    focusedBorder: InputBorder.none,
                    errorBorder: InputBorder.none,
                    hintText: "请输入新的密码",
                    icon: Text("新密码"),
                  ),
                  obscureText: true,
                  validator: (v) {
                    return reg_upwdNew.hasMatch(v!) ? null : "请输入新的密码";
                  },
                ),
              ),
              Container(
                child: const Divider(
                    height: 0,
                    thickness: 1,
                    indent: 15,
                    endIndent: 15,
                    color: Color.fromRGBO(240, 240, 240, 1)),
              ),
              Container(
                padding: const EdgeInsets.fromLTRB(15, 0, 15, 0),
                decoration: const BoxDecoration(
                    color: Color.fromRGBO(255, 255, 255, 1)),
                child: TextFormField(
                  controller: _upwdConfirmCtl,
                  decoration: const InputDecoration(
                    enabledBorder: InputBorder.none,
                    focusedBorder: InputBorder.none,
                    errorBorder: InputBorder.none,
                    hintText: "请输入新的密码",
                    icon: Text("确认密码"),
                  ),
                  obscureText: true,
                  validator: (v) {
                    return reg_upwdConfirm.hasMatch(v!) ? null : "请再次输入新密码";
                  },
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Expanded(
                      child: Container(
                        height: 44,
                        margin: const EdgeInsets.fromLTRB(15, 20, 15, 20),
                        child: ElevatedButton(
                          onPressed: () {
                            if ((_gk.currentState as FormState).validate()) {
                              print(
                                  "源密码：${_upwdSourceCtl.text.trim()},新密码：${_upwdNewCtl.text.trim()},确认新密码：${_upwdConfirmCtl.text.trim()}");
                            }
                          },
                          child: const Text("确定"),
                        ),
                      ))
                ],
              ),
              // ElevatedButton(
              //   style: const ButtonStyle(),
              //   onPressed: () {
              //     if ((_gk.currentState as FormState).validate()) {
              //       print(
              //           "源密码：${_upwdSourceCtl.text.trim()},新密码：${_upwdNewCtl.text.trim()},确认新密码：${_upwdConfirmCtl.text.trim()}");
              //     }
              //   },
              //   child: const Text("确定"),
              // )
            ],
          ),
        ),
      ),
    );
  }
}
